﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DFGK_WMS.Repository
{
    /// <summary>
    /// 承运商
    /// </summary>
    public class CarrierRepository: BaseRepository<TCarrier>, ICarrierRepository
    {
        public CarrierRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool AddCarrier(CarrierDto dto)
        {
            try
            {
                var obj = Context.TCarriers.AsNoTracking().FirstOrDefault(x => x.Code == dto.Code);
                if (obj != null)
                {
                    return false;
                }
                TCarrier carrier = new TCarrier()
                {
                    Code = dto.Code,
                    Name = dto.Name,
                    AddrDetail = dto.AddrDetail,
                    Contact = dto.Contact,
                    ContactMobile = dto.ContactMobile,
                    Contact1 = dto.Contact1,
                    Contact1Mobile = dto.Contact1Mobile,
                    Fax = dto.Fax,
                    Email = dto.Email,
                    Remark = dto.Remark,
                    DisplayOrder = dto.DisplayOrder,
                    CreateDate = DateTime.Now,
                    CreateUserId = dto.NowUserId,
                    CreateUserName = dto.NowUserRealName

                };
                Context.TCarriers.Add(carrier);
                return Context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool UpdateCarrier(CarrierDto dto)
        {
            try
            {
                var oobj = Context.TCarriers.AsNoTracking().FirstOrDefault(x => x.Id != dto.Id && x.Code == dto.Code);

                if (oobj != null)
                {
                    return false;
                }
                TCarrier carrier = new TCarrier()
                {
                    Id=dto.Id,
                    Code = dto.Code,
                    Name = dto.Name,
                    AddrDetail = dto.AddrDetail,
                    Contact = dto.Contact,
                    ContactMobile = dto.ContactMobile,
                    Contact1 = dto.Contact1,
                    Contact1Mobile = dto.Contact1Mobile,
                    Fax = dto.Fax,
                    Email = dto.Email,
                    Remark = dto.Remark,
                    DisplayOrder = dto.DisplayOrder,
                    ModifyDate = DateTime.Now,
                    ModifyUserId = dto.NowUserId,
                    ModifyUserName = dto.NowUserRealName,
                    CreateDate = dto.CreateDate,
                    CreateUserId = dto.NowUserId,
                    CreateUserName = dto.NowUserRealName,

                };
                Context.TCarriers.Update(carrier);
                return Context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DelCarrier(int ids)
        {
            try
            {
                var carr = Context.TCarriers.Find(ids);
                Context.TCarriers.Remove(carr);
                return Context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TCarrier> GetCarriers(string name,int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var list = Context.TCarriers.AsQueryable();
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x => x.Name.Contains(name));
                }
                totalCount = list.Count();
                list = list.OrderBy(x => x.DisplayOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize);          
                return list.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TCarrier GetCarrier(int id)
        {
            return Context.TCarriers.Find(id);
        }
    }
}
